package com.admission.web.admin;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.ArrayInfo;
import com.admission.model.Environment;
import com.admission.model.TeacherView;
import com.admission.service.ArrayInfoManager;
import com.admission.service.EnvironmentManager;
import com.admission.service.TeacherViewManager;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class ArrayInfoAdminAction extends CrudActionSupport<ArrayInfo> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayInfo entity;
	private ArrayInfoManager arrayInfoManager;
	private Page<ArrayInfo> page = new Page<ArrayInfo>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private String id;
	private String ids;
	private String name;
	private String arrayId;
	private ArrayInfo arrayInfo;
	private EnvironmentManager environmentManager;
	private TeacherViewManager teacherViewManager;

	public ArrayInfo getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		page.setOrderBy("orderNo");
		page.setOrder(Page.ASC);
		arrayInfoManager.getByValue(page, name);
		arrayInfo = arrayInfoManager.getByItName(name);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		if (StringUtils.isEmpty(arrayId))
			entity = arrayInfoManager.getByItName(name);
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		try {
			arrayInfoManager.save(entity, arrayId);
			if (StringUtils.isEmpty(arrayId)) {
				this.addActionMessage("操作成功");
				return INPUT;
			}
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return list();
	}

	@Override
	public String delete() throws Exception {
		Environment e = environmentManager.getByType(arrayInfoManager
				.getArrayInfoByid(ids).getItemName());
		List<TeacherView> t = teacherViewManager.getByType(arrayInfoManager
				.getArrayInfoByid(ids).getItemName());
		if (e != null)
			environmentManager.deleteByIds(e.getId());
		if (t != null) {
			for (TeacherView v : t)
				teacherViewManager.deleteMore(v.getId());
		}
		arrayInfoManager.delete(ids);
		if (name == null) {
			Struts2Utils.renderText("true");
			return null;
		} else
			return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new ArrayInfo();
		} else {
			entity = arrayInfoManager.getArrayInfoByid(id);
		}
	}

	public ArrayInfo getEntity() {
		return entity;
	}

	public void setEntity(ArrayInfo entity) {
		this.entity = entity;
	}

	public ArrayInfoManager getArrayInfoManager() {
		return arrayInfoManager;
	}

	@Autowired
	public void setArrayInfoManager(ArrayInfoManager arrayInfoManager) {
		this.arrayInfoManager = arrayInfoManager;
	}

	public Page<ArrayInfo> getPage() {
		return page;
	}

	public void setPage(Page<ArrayInfo> page) {
		this.page = page;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getArrayId() {
		return arrayId;
	}

	public void setArrayId(String arrayId) {
		this.arrayId = arrayId;
	}

	public ArrayInfo getArrayInfo() {
		return arrayInfo;
	}

	public void setArrayInfo(ArrayInfo arrayInfo) {
		this.arrayInfo = arrayInfo;
	}

	public EnvironmentManager getEnvironmentManager() {
		return environmentManager;
	}

	@Autowired
	public void setEnvironmentManager(EnvironmentManager environmentManager) {
		this.environmentManager = environmentManager;
	}

	public TeacherViewManager getTeacherViewManager() {
		return teacherViewManager;
	}

	@Autowired
	public void setTeacherViewManager(TeacherViewManager teacherViewManager) {
		this.teacherViewManager = teacherViewManager;
	}

}
